#pragma once
#include <iostream>
#include <string>
#include <atomic>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>
namespace NS_util
{
    class Timeutil
    {
    public:
        static std::string GetTimestamp()
        {
            // struct timeval _time;
            // gettimeofday(&_time,nullptr);
            // return std::to_string(_time.tv_sec);
            time_t current = time(nullptr);
            struct tm *curr = localtime(&current);

            char time_buffer[1024];
            snprintf(time_buffer, sizeof(time_buffer), "%d-%d-%d %d:%d:%d", curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday, curr->tm_hour, curr->tm_min, curr->tm_sec);
            return time_buffer;
        
        }
        //获得毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }

    };
    const std::string temp_path = "./temp/";

    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            // 形成路径名
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;
            return path_name;
        }
        // 构建源文件路径+后缀完整文件名
        // 123->./temp/1234.cpp
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        // 构建可执行程序的完整路径+后缀名
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name, ".complier_error");
        }
        // 构建该程序对应标准错误完整路径+后缀名
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
        // 运行时需要的临时文件
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat st;
            if (stat(path_name.c_str(), &st) == 0) //?
            {
                return true;
            }
            return false;
        }
        static std::string UniqeFileName()
        {
            static std::atomic_uint id(0);
            id++;
            //获得毫秒时间戳+原子性递增:保证唯一性
            std::string ms = Timeutil::GetTimeMs();
            std::string uniq_id = std::to_string(id);
            return ms + '_' + uniq_id;
        }
        static bool WriteFile(const std::string &target, const std::string &content)
        {
            std::ofstream out(target);
            if(!out.is_open())
            {
                return false;
            }
            out.write(content.c_str(),content.size());
            out.close();
            return true;
        }
        static bool Readfile(const std::string &target,std::string *content,bool keep = false)
        {
            content->clear();
            std::ifstream in(target);
            if(!in.is_open())
            {
                return false;
            }
            std::string line;
            //getline:不保存行分割符
            while(std::getline(in,line))
            {
                (*content) += line;
                (*content) += keep ? "\n" : "";
            }
            in.close();
            return true;
        }
    };
    
    class Stringutil
    {
    public:
        /*
            str:输入字符串，要切分的字符串
            target:输出型，输出切分后的字符串
            sep:字符串怎么分割的
        */
        static void SpiltString(const std::string &str,std::vector<std::string> *target,std::string sep)
        {
            //boost 库split方法
            boost::split((*target),str,boost::is_any_of(sep),boost::algorithm::token_compress_on);
        }
    };
}
